<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Waybill
 *
 * @author wupeng
 */
include_once 'ApiAbstract.php';
include_once 'Rechange.php';

class Waybill extends ApiAbstract {

    //put your code here

    protected $_tradeNo;
    protected $_paybill;
    protected $_domainId = null;

    public function __construct($trade_no, $paybill, $periodStart, $account) {
        $this->_tradeNo = $trade_no;
        $this->_paybill = $paybill;
        parent::__construct($periodStart, $account);
        $user = Application_Model_User::load($this->_paybill->AccountId);
        $this->_fields = array(
            'transaction_number' => $trade_no,
            'trader' => 'backend-account-dr',
            'customer_id' => $paybill->AccountId,
            'member' => 'admin',
            'customer_name' => $user->nikename,
            'amount' => $paybill->Money,
            'currency' => 'CNY',
            'rate' => '1',
            'rate_amount' => '0',
            'period_start' => $periodStart,
            'period_end' => $account->Money,
            'business_type' => 'estimate-ship-fee',
            'order_sn' => ''
        );
    }

    protected function _getWebsiteBusinessFee() {
        return array(
            'EstimateShipFee' => Application_Model_FeeType::EstimateShipFee,
            'isCompress' => Application_Model_FeeType::VacuumSeviceFee,
            'isNoLogo' => Application_Model_FeeType::RemovalFee,
            'isWooden' => Application_Model_FeeType::WoodenFee,
            'CnShipFee' => Application_Model_FeeType::CnShipFee,
            'RemoteFee' => Application_Model_FeeType::RemoteFee,
            'RatePremiumFee' => Application_Model_FeeType::RatePremiumFee,
            'FragileFee' => Application_Model_FeeType::FragileFee
        );
    }

    protected function _getWebsiteBusinessCode() {
        return array(
            'EstimateShipFee' => 'guojiyunfei-dr',
            'isCompress' => 'zhengzhifeiyong-dr',
            'isNoLogo' => 'zhengzhifeiyong-dr',
            'isWooden' => 'zhengzhifeiyong-dr',
            'CnShipFee' => 'guoreiyunfei-dr',
            'RemoteFee' => 'pyfbj-dr',
            'RatePremiumFee' => 'baoxianfei-dr',
            'FragileFee' => 'zhengzhifeiyong-dr'
        );
    }

    protected function _addRechangeInfo() {
        $rechange = new Rechange($this->_tradeNo, $this->_paybill, $this->_periodStart, $this->_account);
        $rechange->addTransaction();
        $account = Application_Model_UserAccount::load($this->_account->id);
        $this->_periodStart = $account->Money;
        return $this;
    }

    protected function _getTrades() {
        return array(
            Application_Model_PayBill::PAY_PAYPAL => 'other_Paypal',
            Application_Model_PayBill::PAY_ALIPAY => 'other_alipay',
            Application_Model_PayBill::PAY_CARD => 'xinyongka',
            Application_Model_PayBill::PAY_ACCOUNT => 'backend-account-dr'
        );
        
    }
    
    public function getDomainId(){
        if(null == $this->_domainId){
            $this->_domainId = $this->_paybill->DomainId;
        }
        return $this->_domainId;
    }
    
    public function setDomainId($domainId){
        $this->_domainId = $domainId;
        return $this;
    }
    

    public function addTransaction() {
        $trades = $this->_getTrades();
        $this->setFields('trader',$trades[$this->_paybill->payId]);
        $periodStart = $this->_periodStart;
        if ($this->_paybill->payId != Application_Model_PayBill::PAY_ACCOUNT) {
            $this->_addRechangeInfo();
            $periodStart = $this->_account->Money;
        }
        $billall = Application_Model_wayBillItem::load($this->getDomainId());
        $websiteBusinessFee = $this->_getWebsiteBusinessFee();
        $businessCode = $this->_getWebsiteBusinessCode();

        $discountFee = $billall->discountFee;
        if (0 < $discountFee) {
            $this->setFields('business_type', 'youhuiquan-dr')
                    ->setFields('trader', 'backend-account-cr')
                    ->setFields('amount', $discountFee)
                    ->setFields('order_sn', $billall->billNo)
                    ->setFields('period_start', $periodStart)
                    ->setFields('period_end', $periodStart + $discountFee);
            $this->_addTransaction();
            $periodStart += $discountFee;
        }
        
        foreach ($websiteBusinessFee as $key => $_business) {
            $fee = $billall->getFeeByType($_business);
            if (0 < $fee) {
                $businessType = $businessCode[$key];
                $this->setFields('business_type', $businessType)
                        ->setFields('trader', 'backend-account-dr')
                        ->setFields('amount', $fee)
                        ->setFields('order_sn', $billall->billNo)
                        ->setFields('period_start', $periodStart)
                        ->setFields('period_end', $periodStart - $fee);
                $this->_addTransaction();
                $periodStart -= $fee;
            }
        }


        return $this;
    }

    protected function _addTransaction() {
        $result = false;
        $fields = $this->getFields();
        if (!empty($fields)) {
            $url = $this->_buildUrl('addTransaction');

            $result = $this->_updateMageFinance($url, $this->getFields());
        }

        return $result;
    }

}
